package com.qububu.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author:ZCW
 * @功能：对properties配置文件的增改查处理
 * @详解：
 * */
public class ReadPrperties {
	
	/**
	 * @author ZCW
	 * 
	 * @功能 读取properties配置文件中的所有内容,转存如Map中
	 * 
	 * @param 
	 * 	fileName: properties文件名
	 * 
	 * @return
	 * 	Map<String, String>properties文件中的所有键和值
	 * */
	public static Map<String, String> captureProperties(String fileName) {
		FileInputStream inStream = null;
		Properties pro = null;
		Map<String, String> propertiesMap = new HashMap<String, String>();
		
		try {
			
			String thisFilePath = ReadPrperties.class.getResource(File.separator).getPath();
			
			inStream = new FileInputStream(thisFilePath.substring(1, thisFilePath.indexOf("build")) + "WebContent/WEB-INF/config" + File.separator + fileName);
			
//			inStream = ReadPrperties.class.getResourceAsStream(fileName);
			
			pro = new Properties();
			
			pro.load(inStream);
			
			inStream.close();
			
			inStream = null;
			
			Enumeration<?> en = pro.propertyNames();
			
			while(en.hasMoreElements()) {
				String key = (String) en.nextElement();
				String value = pro.getProperty(key);
				propertiesMap.put(key, new String(value.getBytes("ISO-8859-1"), "UTF-8"));
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("文件找不到----可能是文件路径(path+fileName)错误");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("装载文件失败!!!!");
		}
		
		return propertiesMap;
	}

	/**
	 * @author ZCW
	 * 
	 * @功能 读取properties配置文件中的所有内容,转存如Map中
	 * 
	 * @param 
	 * 	path: properties文件路径
	 * 	fileName: properties文件名
	 * 
	 * @return
	 * 	Map<String, String>properties文件中的所有键和值
	 * */
	public static Map<String, String> captureProperties(String path, String fileName) {
		FileInputStream inStream = null;
		Properties pro = null;
		Map<String, String> propertiesMap = new HashMap<String, String>();
		
		try {
			
			inStream = new FileInputStream(path + File.separator + fileName);

			pro = new Properties();
			
			pro.load(inStream);
			
			inStream.close();
			
			inStream = null;
			
			Enumeration<?> en = pro.propertyNames();
			
			while(en.hasMoreElements()) {
				String key = (String) en.nextElement();
				String value = pro.getProperty(key);
				propertiesMap.put(key, new String(value.getBytes("ISO-8859-1"), "UTF-8"));
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("文件找不到----可能是文件路径(path+fileName)错误");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("装载文件失败!!!!");
		}
		
		return propertiesMap;
	}
	
	/**
	 * @author ZCW
	 * 
	 * @功能 根据文件路径及文件名读取properties文件中指定key的值
	 * 
	 * @param 
	 * 	path: 文件路径
	 * 	fileName: 文件名
	 * 	key: key名字
	 * 
	 * @return 
	 * 	String: 对应key的值
	 * */
	public static String getPropertiesValueByKey(String path, String fileName, String key) {
		
		InputStream inStream = null;
		Properties pro = null;
		
		String value = "";
		
		String filePath = path + File.separator + fileName;
		
		try {
			
			inStream = new FileInputStream(filePath);
			
			pro = new Properties();
			
			pro.load(inStream);
			
			//如果配置文件包含这个key
			if (pro.containsKey(key)) {
				value = pro.getProperty(key);
				value =  new String(value.getBytes("ISO-8859-1"), "UTF-8");
			} else {
				System.out.println("输入的key不对");
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("文件找不到----可能是文件路径(path+fileName)错误");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("装载文件失败!!!!");
		} finally{
			if(inStream != null) {
				try {
					inStream.close();
					inStream = null;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		return value;
	}
	
	/**
	 * @author ZCW
	 * 
	 * @功能 向properties配置文件存入数据,如果这个key存在则修改这个key对应的value为传入的值
	 * 
	 * @param
	 * 	key: 需要保存的值的key
	 * 	value: 需要保存的值
	 * 	path: properties文件路径
	 * 	fileName: properties文件名
	 * */
	public static void saveOneValueToProperties(String key, String value, String path, String fileName) {
		FileInputStream inStream = null;
		FileOutputStream outStream = null;
		
		Properties pro = null;
		
		String filePath = path + File.separator + fileName;
		
		try {
			
			inStream = new FileInputStream(filePath);
			
			pro = new Properties();
			
			pro.load(inStream);
			
			pro.setProperty(key, value);
			
			outStream = new FileOutputStream(filePath);
			
			pro.store(outStream, "Sorry Idon't know!!!");
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("文件找不到----可能是文件路径(path+fileName)错误");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("装载文件失败!!!!");
		} finally {
			if(inStream != null) {
				try {
					inStream.close();
					inStream = null;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (outStream != null) {
				try {
					outStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * @author ZCW
	 * 
	 * @功能 获取WEB-INF文件夹及其子文件夹物理位置
	 * */
	public static String getConfigFilePath(HttpServletRequest request) {
		
		String path = File.separator + "WEB-INF" + File.separator + "config";
		
		return request.getSession().getServletContext().getRealPath(path);
	}
	
	public static void main(String ars[]) {
//		String path = "qububu/WebContent/WEB-INF/config";
		String fileName = "alipay_config.properties";
//		Map<String, String> map = captureProperties(path, fileName);
		Map<String, String> map = captureProperties(fileName);
		Set<String> set = map.keySet();
		Iterator<String> it = set.iterator(); 
		while(it.hasNext()) {
			String key = it.next();
			String value = map.get(key);
			System.out.print("key= "+key);
			System.out.println("---value= "+value);
		}
	}
}
